<?php
class Catalog extends EMongoDocument {
	public $name = "";
	public $description = "";
	public $icon = "";
	public $image = "";
	public $root = "";

	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	// This method is required!
	public function getCollectionName(){
		return 'tbl_catalog';
	}

	public function rules(){
		$rules = array();
		$rules[] = array("name","required","message" => "Vui lòng nhập tên chuyên mục");
		return $rules;
	}
	
	public function attributeLabels(){
		return array(
				'name' => 'Tên chuyên mục',
				'description' => 'Mô tả',
				'root' => 'Chuyên mục cha'
		);
	}

	public function loadRootCatalog(){
		$data = $this->findAllByAttributes(array("root" => ""));
		$list = CHtml::listData($data,function($post) {
								return CHtml::encode($post['_id'].'');
							},"name");
		$result = array("" => "");
		$result = array_merge($result, $list);
		return $result;
	}
	
	public function search(){
		$cond = new EMongoCriteria;
		if(!empty($this->name)){
			$cond->name = new MongoRegex("/".$this->name."/i");
		}
		return new EMongoDocumentDataProvider('Catalog', array(
				'criteria' => $cond,
				'sort'=>array(
						'attributes'=>array(
								'name'
						)
				),
				'pagination' => array(
					
				)
		));
	}
}
?>